const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        ignoreDefaultArgs: ['--enable-automation'],
        defaultViewport: {
            width: 1200,
            height: 900
        }
    })
    const page = await browser.newPage()
    let content = fs.readFileSync("./urls.txt", 'UTF-8')
    let items = content.split("\r\n")
    for (let item of items) {
        try {
            await page.goto(item, { waitUntil: "networkidle0" })
            page.waitForSelector("div.mark");
            let temp = await page.evaluate(() => {
                const title = document.querySelector("p.title").textContent
                const desc = document.querySelector("p.sub") ? document.querySelector("p.sub").textContent : ""
                const location = document.querySelector("p.location") ? document.querySelector("p.location").textContent : ""
                const url = document.querySelector("div.mark").style.backgroundImage
                return { title, desc, location, url };
            })
            fs.appendFileSync("./data.json", JSON.stringify(temp) + ",");
            console.log(temp)
        } catch (error) {
            continue
        }
    }
    await browser.close()
})()